﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.DataEntity;
using TimKen.BussLogic;
using System.Data;

namespace TimKen.Web.Admin.Job
{
    public partial class Job_Add : TimKen.App.AdminPage
    {
        public override string ActionCode
        {
            get
            {
                return "98";
            }
        }

        public int JobID
        {
            get
            {
                if (ViewState["JobID"] != null)
                    return Convert.ToInt32(ViewState["JobID"]);
                else
                    return 0;
            }
            set
            {
                ViewState["JobID"] = value;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
          
            if (!Page.IsPostBack)
            {
                BindResources();

                if (Request.QueryString["JobID"] != null)
                {
                    JobID = Convert.ToInt32(Request.QueryString["JobID"]);
                }
                if (JobID != 0)
                    BindData();
                else
                    this.txtCreateDate.ReadOnly = true;
            }
        }

        protected void BindResources()
        {
            ddlLanguage.Items.Clear();
            ListItem li_ZH = new ListItem("简体中文", "1");
            ListItem li_EN = new ListItem("English", "2");
            ddlLanguage.Items.Add(li_ZH);
            ddlLanguage.Items.Add(li_EN);

            btnConfirm.Text = GetLangString("Save");
        }

        private void BindData()
        {
            JobManager am = new JobManager();
            DataEntity.Job Job = am.GetModel(JobID);
            this.txtJobNo.Text = Job.JobNo;
            this.txtJobName.Text = Job.JobName;

            this.txtValidFrom.Text = Job.ValidFrom.HasValue?Job.ValidFrom.Value.ToString():"";
            this.txtValidTo.Text = Job.ValidTo.HasValue ? Job.ValidTo.Value.ToString() : "";


            if (ddlLanguage.SelectedItem != null)
                ddlLanguage.SelectedItem.Selected = false;

            if (Job.LanguageID == 1)
                ddlLanguage.Items[0].Selected = true;
            else
                ddlLanguage.Items[1].Selected = true;

            this.fckJobDesc.Value = Job.JobDesc;
            this.FCQualifications.Value = Job.Qualifications;

            this.txtCreateDate.Text = Job.CreateDate.Value.ToString("yyyy-MM-dd");
            this.txtCreateDate.ReadOnly = true;
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            DataEntity.Job Job = new TimKen.DataEntity.Job();
            JobManager am = new JobManager();

            //新增职位
            if (JobID == 0)
            {
                Job.CreateDate = DateTime.Now;
                Job.LanguageID = this.ddlLanguage.SelectedValue == "1" ? 1 : 2;
                Job.JobNo = this.txtJobNo.Text.Trim();
                Job.JobName = this.txtJobName.Text.Trim();
                Job.ValidFrom = Convert.ToDateTime(this.txtValidFrom.Text.Trim());
                Job.ValidTo = Convert.ToDateTime(this.txtValidTo.Text.Trim());
                Job.JobDesc = this.fckJobDesc.Value;
                Job.Qualifications = this.FCQualifications.Value;
                am.Add(Job);
            }
            else
            {//更新职位
                Job.JobID = JobID;
                Job.CreateDate = DateTime.Now;
                Job.LanguageID = this.ddlLanguage.SelectedValue == "1" ? 1 : 2;
                Job.JobNo = this.txtJobNo.Text.Trim();
                Job.JobName = this.txtJobName.Text.Trim();
                Job.ValidFrom = Convert.ToDateTime(this.txtValidFrom.Text.Trim());
                Job.ValidTo = Convert.ToDateTime(this.txtValidTo.Text.Trim());
                Job.JobDesc = this.fckJobDesc.Value;
                Job.Qualifications = this.FCQualifications.Value;
                am.Update(Job);
            }

            Response.Redirect("Job_List.aspx");
        }
    }
}
